package SuperMarketGuis;

import java.sql.*;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;

public class AddProductstoStores extends javax.swing.JDialog {
    
    public static String dbURL = 
	"jdbc:derby://localhost:1527/SuperMarket;create=true;user=plh24;password=plh24";
    public String driver = "org.apache.derby.jdbc.ClientDriver";
    public String storename;
    //public int store_id;
    
    public class add_sub_products  
        {
        /* Βοηθητική κλάση δεδομένων, εσωτερική στο συγκεκριμένο GUI με τους 
        αντίστοιχους Getters/Setters. Δημιουργεί οντότητες της μορφής 
        (String "add" + String productname)  ή οντότητες της μορφής 
        (String "sub" + String productname) ανάλογα με το αν ο χρήστης 
        πάτησε το κουμπί "Προσθήκη" ή "Αφαίρεση" αντίστοιχα. Κάθε φορα που 
        το κάνει δημιουργείται ένα αντίγραφο αυτής της κλάσης και αποθηκεύεται 
        στο δυναμικό πίνακα ArrayList<add_sub_products> aSP */
            private String add_subproduct;
            private String productname;
            private int product_id;

        public String getAdd_subproduct() {
            return add_subproduct;
        }

        public void setAdd_subproduct(String add_subproduct) {
            this.add_subproduct = add_subproduct;
        }

        public String getProductname() {
            return productname;
        }

        public void setProductname(String productname) {
            this.productname = productname;
        }

        public int getProduct_id() {
            return product_id;
        }

        public void setProduct_id(int product_id) {
            this.product_id = product_id;
        }
        
        }
    
    ArrayList<add_sub_products> aSP = new ArrayList<add_sub_products>();
    /* Δυναμικός πίνακας που αποθηκεύει διανύσματα τύπου add_sub_products */
    
    DefaultListModel dlm1 = new DefaultListModel();
    DefaultListModel dlm2 = new DefaultListModel();
    
    public void populateleftlist() 
    {
        dlm1.clear();
        try
            {
                //Get a connection
                Class.forName(driver).newInstance();
                Connection conn = DriverManager.getConnection(dbURL); 
                Statement stmt = conn.createStatement();
                String query1 = ("SELECT name FROM plh24.product order by name");
                ResultSet rst1 = stmt.executeQuery(query1);
                while (rst1.next())
                {
                    dlm1.addElement(rst1.getString(1));
                    
                }
                stmt.close();
                conn.close();
                leftList.setModel(dlm1);
                
            }
        catch (Exception ex)
            {
                System.out.println(ex.getMessage());
            }
        
    }
    
    public void populaterightlist() 
    {
        dlm2.clear();
        try
            {
                //Get a connection
                Class.forName(driver).newInstance();
                Connection conn = DriverManager.getConnection(dbURL); 
                Statement stmt = conn.createStatement();
                String query2 = ("select product.name,store.name from product,store_product,store "
                        + "where product.product_id = store_product.product "
                        + "and store_product.store = store.store_id and store.name like '"
                        +storename+"' order by product.name");
                ResultSet rst2 = stmt.executeQuery(query2);
                while (rst2.next()) 
                {
                    dlm2.addElement(rst2.getString(1));
                    dlm1.removeElement(rst2.getString(1));
                    
                }
                stmt.close();
                conn.close();
                rightList.setModel(dlm2);
            }
        catch (Exception ex)
            {
                System.out.println(ex.getMessage());
            }
    }
    
    public void comboboxStoreList() //Combobox
    {
        try
            {
                //Get a connection
                Class.forName(driver).newInstance();
                Connection conn = DriverManager.getConnection(dbURL); 
                Statement stmt = conn.createStatement();
                String query = ("SELECT name FROM plh24.store order by name");
                ResultSet rst = stmt.executeQuery(query);
                while(rst.next())
                {
                    storeSelect.addItem(rst.getString(1));
                }
                stmt.close();
                conn.close();
            }
        catch (Exception ex)
            {
                MessageArea.setText(ex.getMessage());
            }
    }
    
    public AddProductstoStores(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        
        initComponents();
        comboboxStoreList();
        populateleftlist();
        populaterightlist();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        storeSelect = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        leftList = new javax.swing.JList();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        rightList = new javax.swing.JList();
        jPanel4 = new javax.swing.JPanel();
        bAddProduct = new javax.swing.JButton();
        bRemoveProduct = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        bAcceptChanges = new javax.swing.JButton();
        bCancelChanges = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        MessageArea = new javax.swing.JTextArea();
        bReturn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);

        jLabel1.setFont(new java.awt.Font("Calibri", 1, 18)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Συσχέτιση Προϊόντων και Καταστημάτων.");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        storeSelect.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        storeSelect.setMaximumRowCount(5);
        storeSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                storeSelectActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Επιλέξτε Κατάστημα");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(storeSelect, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(185, 185, 185))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(storeSelect, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        leftList.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        leftList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(leftList);
        int cellWidth = 250;
        leftList.setFixedCellWidth(cellWidth);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 373, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        rightList.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        rightList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(rightList);
        rightList.setFixedCellWidth(cellWidth);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2)
                .addContainerGap())
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));

        bAddProduct.setText("Προσθήκη");
        bAddProduct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAddProductActionPerformed(evt);
            }
        });

        bRemoveProduct.setText("Αφαίρεση");
        bRemoveProduct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bRemoveProductActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bAddProduct, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bRemoveProduct, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(13, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(73, 73, 73)
                .addComponent(bAddProduct)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(bRemoveProduct)
                .addGap(136, 136, 136))
        );

        jLabel3.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Διαθέσιμα Προϊόντα");

        jLabel4.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("Προϊόντα Καταστήματος");

        bAcceptChanges.setText("Αποδοχή");
        bAcceptChanges.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAcceptChangesActionPerformed(evt);
            }
        });

        bCancelChanges.setText("Ακύρωση");
        bCancelChanges.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCancelChangesActionPerformed(evt);
            }
        });

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        MessageArea.setEditable(false);
        MessageArea.setColumns(1);
        MessageArea.setFont(new java.awt.Font("Calibri", 0, 16)); // NOI18N
        MessageArea.setForeground(new java.awt.Color(255, 0, 0));
        MessageArea.setRows(6);
        MessageArea.setText("1) Για να μεταβάλετε τα διαθέσιμα προϊόντα ενός καταστήματος επιλέξτε το από τη λίστα και \n     χρησιμοποιείστε τα πλήκτρα \"Προσθήκη\" και \"Αφαίρεση\" για να κάνετε τις αλλαγές που θέλετε.\n2) Για να καταχωρήσετε τις αλλαγές πατήστε το πλήκτρο \"Αποδοχή\".\n3) Για να ακυρώσετε τις αλλαγές που κάνατε πατήστε το πλήκτρο \"Ακύρωση\".\n4) Πατήστε το πλήκτρο \"Επιστροφή\" για να γυρίσετε στο προηγούμενο μενού.");
        jScrollPane3.setViewportView(MessageArea);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
                .addContainerGap())
        );

        bReturn.setText("Επιστροφή");
        bReturn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bReturnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(101, 101, 101)
                .addComponent(bAcceptChanges)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(bCancelChanges)
                .addGap(94, 94, 94))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(bReturn)
                .addGap(331, 331, 331))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bAcceptChanges)
                    .addComponent(bCancelChanges))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bReturn)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bAddProductActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAddProductActionPerformed
        if (leftList.getSelectedIndex()!=-1)
        {
            int product_id = 0;
            try
            {
            //Get a connection
                Class.forName(driver).newInstance();
                Connection conn = DriverManager.getConnection(dbURL); 
                Statement stmt = conn.createStatement();
                String sqlquery = ("SELECT product_id FROM plh24.product where name like '"
                        +leftList.getSelectedValue().toString()+"'");
                ResultSet rs1 = stmt.executeQuery(sqlquery);
                while(rs1.next())
                {
                    product_id = rs1.getInt(1);
                }
                stmt.close();
                conn.close();
            }
            catch (Exception ex)
            {
                MessageArea.setText(ex.getMessage());
                
            }
            add_sub_products pr1 =  new add_sub_products();
            pr1.setAdd_subproduct("add");
            pr1.setProductname(leftList.getSelectedValue().toString());
            pr1.setProduct_id(product_id);
            aSP.add(pr1);
            dlm2.addElement(leftList.getSelectedValue().toString());
            dlm1.removeElement(leftList.getSelectedValue().toString());
        }
        else
        {
            MessageArea.setText("Πρέπει να επιλέξετε ένα προϊόν από την αριστερή λίστα.");
        }
    }//GEN-LAST:event_bAddProductActionPerformed

    private void bRemoveProductActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bRemoveProductActionPerformed
        
        if (rightList.getSelectedIndex()!=-1)
        {
            int product_id = 0;
            try
            {
            //Get a connection
                Class.forName(driver).newInstance();
                Connection conn = DriverManager.getConnection(dbURL); 
                Statement stmt = conn.createStatement();
                String sqlquery = ("SELECT product_id FROM plh24.product where name like '"
                        +rightList.getSelectedValue().toString()+"'");
                ResultSet rs1 = stmt.executeQuery(sqlquery);
                while(rs1.next())
                {
                    product_id = rs1.getInt(1);
                }
                stmt.close();
                conn.close();
            }
            catch (Exception ex)
            {
                MessageArea.setText(ex.getMessage());
            }
            add_sub_products pr1 =  new add_sub_products();
            pr1.setAdd_subproduct("sub");
            pr1.setProductname(rightList.getSelectedValue().toString());
            pr1.setProduct_id(product_id);
            aSP.add(pr1);
            dlm1.addElement(rightList.getSelectedValue().toString());
            dlm2.removeElement(rightList.getSelectedValue().toString());
        }
        else
        {
            MessageArea.setText("Πρέπει να επιλέξετε ένα προϊόν από τη δεξιά λίστα.");
        }        
    }//GEN-LAST:event_bRemoveProductActionPerformed

    private void bAcceptChangesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAcceptChangesActionPerformed
            
        try
        {     
        Class.forName(driver).newInstance();
        Connection conn = DriverManager.getConnection(dbURL); 
        Statement stmt = conn.createStatement();
        String sql="Select store_id from plh24.store where name like '"
                +storeSelect.getSelectedItem().toString()+"'";
        ResultSet rst = stmt.executeQuery(sql);
        int rowaffected,store_id=0;
        while (rst.next())
        {store_id =rst.getInt(1);}
         
        for (int i=0;i<aSP.size();i++)
        {
            if (aSP.get(i).add_subproduct.equalsIgnoreCase("add"))
            {
                sql = "INSERT INTO PLH24.STORE_PRODUCT (PRODUCT, STORE) VALUES ("
                        +aSP.get(i).product_id+", "+store_id+")";
                rowaffected = stmt.executeUpdate(sql);
            }
            else 
            {
                sql = "DELETE FROM PLH24.STORE_PRODUCT WHERE PRODUCT = "
                        +aSP.get(i).product_id+" AND STORE = "+store_id;
                rowaffected = stmt.executeUpdate(sql);
            }
            System.out.println(aSP.get(i).add_subproduct+"  "+aSP.get(i).productname
                    +"  "+aSP.get(i).product_id);
        }
        stmt.close();
        conn.close();
        aSP.clear();  //καθαρίζει τη λίστα ενεργειών
        System.out.println("store_id = "+store_id);
        }
        catch(Exception ex)
        {
            MessageArea.setText(ex.getMessage());
        }
            
    }//GEN-LAST:event_bAcceptChangesActionPerformed

    private void bCancelChangesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelChangesActionPerformed
        populateleftlist();
        populaterightlist();
        aSP.clear();
    }//GEN-LAST:event_bCancelChangesActionPerformed

    private void bReturnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bReturnActionPerformed
        this.dispose();
    }//GEN-LAST:event_bReturnActionPerformed

    private void storeSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_storeSelectActionPerformed
        storeSelect = (JComboBox) evt.getSource();
        storename = storeSelect.getSelectedItem().toString();
        //MessageArea.setText(storename);
        populateleftlist();
        populaterightlist();
        
    }//GEN-LAST:event_storeSelectActionPerformed

    public void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AddProductstoStores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AddProductstoStores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AddProductstoStores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AddProductstoStores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                AddProductstoStores dialog = new AddProductstoStores(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
                
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea MessageArea;
    private javax.swing.JButton bAcceptChanges;
    private javax.swing.JButton bAddProduct;
    private javax.swing.JButton bCancelChanges;
    private javax.swing.JButton bRemoveProduct;
    private javax.swing.JButton bReturn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JList leftList;
    private javax.swing.JList rightList;
    private javax.swing.JComboBox storeSelect;
    // End of variables declaration//GEN-END:variables
}